// comps/uploader/uploader.js
import {
  Config
} from '../../utils/config.js'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    attachmentType: {
      type: Number,
      value: 0
    },
    slug: {
      type: String,
      value: 'testSlug'
    }
  },

  /**
   * 所在页面生命周期
   */
  pageLifetimes: {
    show: function() {
      this._refreshSign();
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    fileList: [{
        uid: 0,
        status: 'uploading',
        url: 'http://cdn.skyvow.cn/qrcode.jpg',
      },
      {
        uid: 1,
        status: 'done',
        url: 'http://cdn.skyvow.cn/qrcode.jpg',
      },
      {
        uid: 2,
        status: 'error',
        url: 'http://cdn.skyvow.cn/qrcode.jpg',
      }
    ],
    curFormData: null,
    host: null,
    //oss信息
    _ossInfo: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 刷新oss签名
     */
    _refreshSign() {
      const token = wx.getStorageSync("token")

      var that = this;
      wx.request({
        url: Config.restUrl + "/cloud/ossInit?type=" + this.data.attachmentType,
        header: {
          'cojwt': token
        },
        success(response) {
          var res = response.data
          if (res.state == "ok") {
            res.payload.files = res.files
            res.payload.mid = res.mid

            that.setData({
              host: res.payload.host,
              _ossInfo: res.payload
            })
          }
        }
      })
    },
    onUploadBefore: function(e) {
      var payload = this.data._ossInfo
      if (payload == null) {
        return null
      }
      if (payload.files == null) {
        return null
      }
      var fl = payload.files.pop()
      // var formData = new FormData()
      var path = payload.dir + fl.name + ".jpg"

      var formData = {
        'OSSAccessKeyId': payload.accessid,
        'policy': payload.policy,
        'Signature': payload.signature,
        'key': path,
        'success_action_status': 200,
        'callback': payload.callback,
        'x:fname': fl.name,
        'x:fcode': fl.code,
        'x:faid': this.data.slug,
        'x:fmid': payload.mid,
        'x:fatype': this.data.attachmentType
      }

      // formData.append("OSSAccessKeyId", payload.accessid); //accessKeyId
      // formData.append("policy", payload.policy); //policy
      // formData.append("Signature", payload.signature); //签名
      // //注意formData里append添加的键的大小写
      // //如果是base64文件，那么直接把base64字符串转成blob对象进行上传就可以了
      // formData.append("key", path); //存储在oss的文件路径
      // formData.append("success_action_status", 200); //成功后返回的操作码
      // // formData.append("x-oss-security-token", payload.signature);
      // formData.append("callback", payload.callback);
      // formData.append("x:fname", fl.name);
      // formData.append("x:fcode", fl.code);
      // formData.append("x:faid", '');
      // formData.append("x:fmid", payload.mid);
      // formData.append("x:fatype", this.data.attachmentType);
      // // formData.append("file", e);

      this.setData({
        curFormData: formData
      })
    },
    onPreview(e) {
      console.log('onPreview', e)
      const {
        file,
        fileList
      } = e.detail
      wx.previewImage({
        current: file.url,
        urls: fileList.map((n) => n.url),
      })
    },

    onChange(e) {
      console.log('onChange', e)
      const {
        file
      } = e.detail
      if (file.status === 'uploading') {
        this.setData({
          progress: 0,
        })
        wx.showLoading()
      } else if (file.status === 'done') {
        this.setData({
          imageUrl: file.url,
        })
      }
    },
    onSuccess(e) {
      console.log('onSuccess', e)
    },
    onFail(e) {
      console.log('onFail', e)
    },
    onComplete(e) {
      console.log('onComplete', e)
      wx.hideLoading()
    },
    onProgress(e) {
      console.log('onProgress', e)
      this.setData({
        progress: e.detail.file.progress,
      })
    },
    onRemove(e) {
      const {
        file,
        fileList
      } = e.detail
      wx.showModal({
        content: '确定删除？',
        success: (res) => {
          if (res.confirm) {
            this.setData({
              fileList: fileList.filter((n) => n.uid !== file.uid),
            })
          }
        },
      })
    }

  }
})